
use BORNdata.dta,clear

*Generating new month and quarter variables
egen newmonth=group(birthyear birthmonth)
egen quarter=cut(newmonth), at(1(3)84) icodes


*Treatment
ge phase1=startyear==2009
ge phase2=startyear==2012
ge phase3=startyear==2013

ge treatment=phase1==1&quarter>7
replace treatment=1 if phase2==1&quarter>19
replace treatment=1 if phase3==1&quarter>19

ge post1=quarter>7
ge post2=quarter>19
ge post3=quarter>19

*Treatment exposure measured in quarters and years
ge intreatment=0
replace intreatment=quarter-7 if startyear==2009&quarter>7
replace intreatment=quarter-19 if startyear==2012&(quarter>19)
replace intreatment=quarter-19 if startyear==2013&(quarter>19)
egen test=group(treated intreatment)
qui tab test, ge(test_)


ge intreatmentyear=0
replace intreatmentyear=birthyear-2009 if startyear==2009&birthyear>2009
egen test2=group(treated intreatmentyear)
qui tab test2,ge(test2_)

*Analysis sample definition
ge intsample=newmonth>12 /*dropping 2008 births because of an issue with the survey instrument*/
replace intsample=. if startyear==2012&quarter>16 /*drop the first phase of SURE-P when it enters treatment*/
replace intsample=. if quarter>19 /*exclude births after 2012 because no control group exists*/

*Macros
global control  multiplebirth safe ageatbirth  wealthindex  married literate muslim malechild   
global access providerpresent
global prenatal antenatalcareany fourplusvisits antenatalstudyclinic 
global delivery facility deliverclinic delivermidwifedoctor
global postnatal postnatalcheck DPT polio MMR BCG
global infmort mort7 mort30 
global maternalout intracomp postcomp


* DESCRIPTIVE GRAPHS
ge quarternew=quarter if intsample==1
replace quarternew=quarternew-4

foreach var in $access $prenatal $delivery $postnatal {
local vtext: variable label `var'
twoway (lpoly `var' quarternew if phase1==1&intsample==1,bw(0.5)) (lpoly `var' quarternew if phase1==0&intsample==1,bw(0.5)),xline(4) xtitle(Quarters) ytitle(`vtext') scheme(s1mono) legend(order(1 "Treated" 1 "Control"))
graph save `var'.gph,replace
}

*REGRESSIONS

*Main effects
foreach var in $access $prenatal $delivery $postnatal $infmort $maternalout  {

areg `var' treatment i.newmonth $control if intsample==1, r cl(facilityid) absorb(facilityid)

areg `var' test2_3-test2_5 treated i.newmonth $control  if intsample==1, r cl(facilityid) absorb(facilityid)

}

*Heterogeneous treatment effects

foreach var in $prenatal $delivery $postnatal {
areg `var' treatment  treatment_*  treated_* post1_* post2_*  i.newmonth $control if intsample==1,r cl(facilityid) absorb(facilityid)
}
 
 

*PLOTTING TREATMENT EFFECTS OVER TIME

foreach var in $access $prenatal $delivery $postnatal {
qui areg `var' test_12-test_22 phase1 phase2 phase3 i.quarter  if intsample==1, r cl(facilityid) absorb(facilityid)
local vtext: variable label `var'
parmest,saving(test`var',replace)
preserve
use test`var',clear
keep in 1/11
ge mon=_n
save,replace
serrbar estimate stderr mon,scale(1.95) yline(0) xtitle(Quarters in treatment) ytitle(Treatment effect) title(`vtext') ylabel(-0.4 -0.2 0 0.2 0.4) xlabel(0(4)12) scheme(s1mono)
graph save `var'parm.gph,replace
restore 
}
